//
//  ViewController.m
//  进度条
//
//  Created by KL on 16/11/17.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()<NSURLSessionDataDelegate>
{
    NSMutableData *imgData;
}

@property (weak, nonatomic) IBOutlet UIProgressView *imgPro;

@property (weak, nonatomic) IBOutlet UIImageView *imgV;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _imgV.backgroundColor = [UIColor grayColor];
    
}
- (IBAction)onClick:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://tupian.enterdesk.com/2014/mxy/01/23/3/2.jpg"];
    NSURLSession *sess = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [sess dataTaskWithURL:url];
    [task resume];
    self.imgPro.progress = 0;
    self.imgV.image = nil;
    
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    self.imgV.image = [UIImage imageWithData:imgData];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    [self.imgPro setProgress:(float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive animated:YES];
    if (!imgData) {
        imgData = [NSMutableData new];
    }
    [imgData appendData:data];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
